/**
 * Title: Hyperbolic engine
 * Description: jFamilyTree Engine
 * Version: provider
 * Copyright: (c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 * Update: Mon Mar 10 00:00:00 CEST 2008
 */
package jFamilyTree.component;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

/**
 * Progress panel
 * 
 * @author Bernard Bou
 */
public class Progress extends JPanel
{
	private static final long serialVersionUID = 1L;

	/**
	 * Stop icon
	 */
	static private Icon theStopIcon = new ImageIcon(Progress.class.getResource("images/stop.png"));

	/**
	 * Working icon
	 */
	static private Icon theWorkingIcon = new ImageIcon(Progress.class.getResource("images/jft_loader.gif"));

	/**
	 * Progress bar
	 */
	private JProgressBar theProgress;

	/**
	 * Label
	 */
	private JLabel theLabel;

	/**
	 * Label
	 */
	private JLabel theText;

	/**
	 * Constructor
	 */
	public Progress()
	{
		super();

		setBackground(Color.WHITE);

		theProgress = new JProgressBar(0, 100);
		theProgress.setMaximumSize(new Dimension(200, 32));
		theProgress.setAlignmentX(Component.CENTER_ALIGNMENT);
		theProgress.setStringPainted(true);
		theProgress.setVisible(false);

		theLabel = new JLabel();
		theLabel.setText("jFamilyTree");
		theLabel.setFont(new Font("Dialog", Font.BOLD, 20));
		theLabel.setPreferredSize(new Dimension(300, 80));
		theLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
		theLabel.setHorizontalAlignment(JLabel.CENTER);
		theLabel.setVerticalTextPosition(SwingConstants.TOP);
		theLabel.setHorizontalTextPosition(SwingConstants.CENTER);
		
		theText = new JLabel();
		theText.setText("starting");
		theText.setFont(new Font("Dialog", Font.PLAIN, 10));
		theText.setPreferredSize(new Dimension(300, 32));
		theText.setAlignmentX(Component.CENTER_ALIGNMENT);
		theText.setBackground(Color.BLUE);
		
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		add(Box.createVerticalGlue());
		add(theLabel);
		add(theText);
		add(theProgress);
		add(Box.createVerticalGlue());
	}

	/**
	 * Put message
	 * 
	 * @param thisMessage
	 *        message
	 */
	public void put(final String thisMessage, final boolean fail)
	{
		Runnable thisRoutine = new Runnable()
		{
			public void run()
			{
				theText.setText(thisMessage);
				theLabel.setIcon(fail ? theStopIcon : theWorkingIcon);
				theProgress.setValue(fail ? 0 : theProgress.getValue() + 10);
				theProgress.setVisible(theProgress.getValue() > 0);
			}
		};
		SwingUtilities.invokeLater(thisRoutine);
	}
}
